<?php

class MyUrlManager extends CUrlManager {

    public function createUrl($route, $params = array(), $ampersand = '&') {
//        if ($route == 'job/detail' | $route == 'job/redirect' | $route == 'job/save') {
//            $params['id'] = hexID($params['id']);
//        } elseif ($route == 'search/result') {            
//            if ($this->checkParams($params, 'kw') == 1) {
//                $route = 'search/keyword';
//            }elseif ($this->checkParams($params, 'lname') == 1) {
//                $route = 'search/location';
//            }elseif ($this->checkParams($params, 'kw,lname') == 1) {
//                $route = 'search/keywordlocation';
//            } elseif ($this->checkParams($params, 'cat,alias') == 1) {
//                $route = 'search/cate';
//            } else {
//                $route = 'search/result';
//            }
//        }

        return parent::createUrl($route, $params, $ampersand);
    }

    /**
     *
     * @param type $params
     * @param type $list_field
     * @return int
     * -1 so luong phan tu cua mang nhieu hon hoac it hon mong muon => 'j' => 'search/all',
     * -2 so luong phan tu = nhau nhung ko giong nhau => 'j' => 'search/all'
     * 1 so luong phan tu giong nhau va bang nhau => router tuong ung
     */
    private function checkParams($params, $list_field) {
        $field = explode(',', $list_field);
        $count = count($field);
        if (count($params) != $count) {
            return -1;
        }
        //kiem tra cac phan tu co ton tai day du hay ko
        $not_exits = false;
        foreach ($field as $key) {
            if (!isset($params[$key])) {
                $not_exits = true;
                break;
            }
        }
        if ($not_exits == true) {
            return -2;
        }
        return 1;
    }

}